import Vue from 'vue'
// 导入Vuex
import Vuex from 'vuex'
//安装插件
Vue.use(Vuex)

const moduleA = {
  state: {
    name: 'hello Vue!',
  },
  mutations: {
    updateName(state) {
      state.name = 'hello world!'
    }
  },
  actions: {
    actionsName(context) {
      setTimeout(() => {
        context.commit('updateName')
      })
    }
  },
  getters: {
  }
}

//1.创建对象
const store = new Vuex.Store({
  state: {
    counter: 10,
    students:[
      {id: 1, name: 'pul', age: 35},
      {id: 2, name: 'irving', age: 29},
      {id: 3, name: 'kobe', age: 40},
      {id: 4, name: 'lizuhu', age: 19}
    ],
    info: {
      name: 'lizuhu',
      age: 19,
      score: 100
    }
  },
  mutations: {
    increment(state) {
      state.counter++
    },
    decrement(state) {
      state.counter--
    },
    addCount(state,payload) {
      console.log(payload.count);
      state.counter += payload.count
    },
    addStu(state,student) {
      state.students.push(student)
    },
    updateInfo(state) {
      //修改对象里的值
      state.info.name = 'hello'
      //修改不在对象里的值，做不到响应式
      // state.info.address = 'shenzhen'
      //对那些不是响应式的属性添加到响应式系统
      // Vue.set(state.info,'address','shenzhen');
      //从对象中删除数据
      // Vue.delete(this.state.info,"age")

      //不能像一下的方式进行异步操作
    //   setTimeout(() => {
    //     state.info.name = 'hello'
    //   })
    }
  },
  actions: {
    actionsInfo(context,payload) {
      setTimeout(() => {
        context.commit('updateInfo');
        console.log(payload.message);
        payload.success()
      },1000)
    }
  },
  getters: {
    powerCounter(state) {
      return state.counter * state.counter
    },
    age(state) {
      return state.students.filter(s => {
        return s.age > 25
      })
    }

  },
  modules: {
    a: moduleA
  }
})

//导出
export default store
